angular.module('app').controller('searchCtrl', ['$http', '$scope', 'dict', function ($http, $scope, dict) {
    ajax_pos();
    $scope.filterObj = {};
    $scope.search = function () {
        ajax_pos({all: $scope.searchKey});
    }
    $scope.sheet = {};
    $scope.tabList = [{id: 'city', name: '城市'}, {id: 'salary', name: '薪资'}, {id: 'scale', name: '规模'}];
    var tabCurrentIndex = "";
    var tabCurrentID = "";
    $scope.tClick = function (item, index) {
        tabCurrentIndex = index;
        tabCurrentID = item.id;
        $scope.sheet.list = dict[item.id];
        $scope.sheet.visable = true;
    }
    $scope.sheet.click = function (item) {
        $scope.sheet.visable = false;
        if (item.id) {
            angular.forEach($scope.tabList, function (val) {
                if (val.id == tabCurrentID) {
                    val.name = item.name;
                }
            });
            var filterKey = tabCurrentID+"Id";

            $scope.filterObj[filterKey] = item.id;
        } else {
            var filterKey = tabCurrentID+"Id";
            delete $scope.filterObj[filterKey];
            angular.forEach($scope.tabList, function (val) {
                if (val.id == tabCurrentID) {
                    switch (tabCurrentID){
                        case "city":
                            val.name = "城市";
                            break;
                        case "salary":
                            val.name = "薪资";
                            break;
                        case "scale":
                            val.name = "公司规模";
                            break;
                    }
                }

            });
        }

        console.log($scope.filterObj);
        /*var param = {};
        if (item.id) {
            param = {id: item.id, key: tabCurrentID + "Id"};
        } else {
            param = "";
        }
        ajax_pos(param);*/
    }
    function ajax_pos(params) {
        $http({
            url: 'data/positionList.json',
            method: 'GET',
            params: params,
            //data:params
        }).then(function (res) {
            $scope.positionList = [];
            if (params && !params.all) {
                for (var i = 0, len = res.data.length; i < len; i++) {
                    var item = res.data[i];
                    if (item[params.key] == params.id) {
                        $scope.positionList.push(item);
                    }
                }
            } else {
                $scope.positionList = res.data;
            }

        }, function (error) {
            console.log(error);
        });
    }
}]);

